<template>
    <orh-trees>
        <p slot="trees">
            <Input v-model="seartxt" size="small" placeholder="搜索人员"  @on-change="toSearch(seartxt)"/>
            <Select placeholder="职务搜索" @on-change="selectDuties" clearable>
                <Option v-for="item in dutiesList" :value="item.id" :key="item.value">
                    {{item.duties_name}}
                </Option>
            </Select>
        </p>
        <Tree :data="trees" slot="trees" :load-data="loadData" @on-select-change="getNodes"></Tree>
        <orh-sub-table slot="content">
            <div slot="table">
                <div>待选人员{{checkNum}}</div>
                <div style="border-bottom: 1px solid #e9e9e9;padding-bottom:6px;margin-bottom:6px;">
                    <Checkbox
                        :indeterminate="indeterminate"
                        :value="checkAll"
                        @click.prevent.native="handleCheckAll">全选</Checkbox>
                </div>
                <CheckboxGroup v-model="checkAllGroup" @on-change="checkAllGroupChange">
                    <Checkbox v-for="item in examiners" :label="item.user_id" :key="item.user_id">
                            {{item.teacher_name.zh?item.teacher_name.zh:item.teacher_name.mn}}
                        </Checkbox>
                </CheckboxGroup>
            </div>            
        </orh-sub-table>
    </orh-trees>
</template>

<script>
    export default {
        props:['newData'],
        data() {
            return {
                prompt: false, //批量删除的弹框
                transfer: true,
                cont: '您确定要删除这些数据吗',
                del: [], //删除
                ClassList: [],
                dutiesList:[],
                selectedUser:[],
                examiners:[],
                tableload: true,
                checkNum:0,
                seartxt:'',
                loading: true,
                trees: [],            
                indeterminate: true,
                checkAll: false,
                checkAllGroup: [],
            }
        },
        methods: {   
            //加载一级部分
            load() {
                this.$http.get('api/user/v1/department/info').then(res=> {
                    this.trees = this.process(res.data)
                }).catch(err=> {
                    this.$Message.error('部门加载失败！')
                })
            },
            // 处理数据
            process(data) {
                let arr = []
                data.forEach(item => {
                    item.title = item.label = item.department_name.zh?item.department_name.zh:item.department_name.mn;
                    item.value = item.id
                    if(item.childs) {
                        item.loading = false,
                        item.children = []
                    }
                    arr.push(item)
                })
                return arr
            },
            //人员搜索
            toSearch(txt) {
                this.checkNum = 0
                if(txt.length!=0) {
                    this.examiners = this.examiners_copy.filter(item=> {
                        return new RegExp(txt).test(item.teacher_name)
                    })
                    this.checkNum = this.examiners.length                   
                } else {
                    this.examiners = this.examiners_copy
                }
            },
            // 异步加载二级部门
            loadData(item, callback) {
                this.$http.get('api/user/v1/department/info?pid='+item.id).then(res=> {
                    callback(this.process(res.data))
                }).catch(err=> {
                    this.$Message.error('子部门加载失败！')
                })
            },
            //部分筛选人员
            getNodes(item) {
                this.examiners = []
                this.checkNum = 0
                let url = `api/user/v1/get/teacher/user/list?department_id=[${item[0].id}]`
                this.$http.get(url).then(res=>{
                    this.examiners = res.data
                    this.checkNum = this.examiners.length                  
                }).catch(err =>{
                    this.$Message.error('人员获取失败！')
                })
            },
            //职务数据
            dutiesData(){
                this.$http.get('api/user/v1/get/duties/list').then(val=>{
                    this.dutiesList=val.data
                })
            },
            //职务筛选人员
            selectDuties(val){
                this.examiners = []
                this.checkNum = 0
                let url = `api/user/v1/get/teacher/user/list?duties_id=[${val}]`
                this.$http.get(url).then(res=>{
                    this.examiners = res.data
                    this.checkNum = this.examiners.length
                    
                }).catch(err =>{
                    this.$Message.error('人员获取失败！')
                })
            },
            remove(data){
                this.tableload = true
                this.checkAllGroup =this.checkAllGroup.slice(data,1)
                this.tableload = false
            },
            //全部人员
            getExamine(){             
                this.checkNum = 0  
                this.$http.get('api/user/v1/get/teacher/user/list').then(val=>{                  
                    this.examiners = val.data
                    this.examiners_copy=val.data
                    this.checkNum = this.examiners.length                  
                }).catch(err =>{
                    this.$Message.error('人员获取失败！')
                })
            },
            //点击全选
            handleCheckAll () {
                if (this.indeterminate) {
                    this.checkAll = false;
                } else {
                    this.checkAll = !this.checkAll;
                }
                this.indeterminate = false;

                if (this.checkAll) {
                    this.examiners.forEach((el,i) => {                     
                        this.checkAllGroup.push(el.user_id)
                    })
                    
                    this.$emit('user-list',this.checkAllGroup)
                } else {
                    this.checkAllGroup = [];
                }
            },
            //选中之后的事件
            checkAllGroupChange (data) {console.log(data);
                if (data.length == this.examiners.length) {
                    this.indeterminate = false;
                    this.checkAll = true;
                    this.$emit('user-list',data)
                } else if (data.length > 0) {                    
                    this.indeterminate = true;
                    this.checkAll = false;
                    this.$emit('user-list',data)
                } else {
                    this.indeterminate = false;
                    this.checkAll = false;
                }                
            },      
        },
        created() {
            this.load();
            this.getExamine()
            this.dutiesData()
            
        },
        mounted() {            
             if(this.newData.length>0){
                this.checkAllGroup=this.newData
             }
        },
    }
</script>

